﻿///////////////////////////////////////////////////////////////////////////////////////////////
// System.Text.Parsing - © 2011 Sebastien Pellizzari
// Released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
///////////////////////////////////////////////////////////////////////////////////////////////

namespace System.Text.Parsing
{
    /// <summary>Represents the signature of a method called by a grammar processor when a message is emitted.</summary>
    /// <param name="sender">The object that emitted the message.</param>
    /// <param name="isError">Determines whether it is an error message.</param>
    /// <param name="sourceInfo">The range of source data that caused the message.</param>
    /// <param name="text">The message text.</param>
    public delegate void GrammarProcessorMessageEvent(GrammarProcessorBase sender, bool isError, GrammarSourceRange sourceInfo, string text);

    /// <summary>Represents the base class for an object that processes grammars and generates code and data.</summary>
    public abstract class GrammarProcessorBase
    {
        //-----------------------------------------------------------------------
        //     Events
        //-----------------------------------------------------------------------

        /// <summary>Occurs when the processor emits a message.</summary>
        public event GrammarProcessorMessageEvent Message;

        //-----------------------------------------------------------------------
        //     Non Public
        //-----------------------------------------------------------------------

        /// <summary>Emits an error message.</summary>
        protected void EmitError(string message)
        {
            if (message == null) throw new ArgumentNullException("message");
            if (Message != null) Message(this, true, GrammarSourceRange.Unknown, message);
        }

        /// <summary>Emits an error message.</summary>
        protected void EmitError(GrammarSourceRange sourceInfo, string message)
        {
            if (message == null) throw new ArgumentNullException("message");
            if (Message != null) Message(this, true, sourceInfo, message);
        }

        /// <summary>Emits an error message.</summary>
        protected void EmitError(string messageFormat, params object[] args)
        {
            if (messageFormat == null) throw new ArgumentNullException("messageFormat");
            if (Message != null) Message(this, true, GrammarSourceRange.Unknown, String.Format(messageFormat, args));
        }

        /// <summary>Emits an error message.</summary>
        protected void EmitError(GrammarSourceRange sourceInfo, string messageFormat, params object[] args)
        {
            if (messageFormat == null) throw new ArgumentNullException("messageFormat");
            if (Message != null) Message(this, true, sourceInfo, String.Format(messageFormat, args));
        }

        /// <summary>Emits a warning.</summary>
        protected void EmitWarning(string message)
        {
            if (message == null) throw new ArgumentNullException("message");
            if (Message != null) Message(this, false, GrammarSourceRange.Unknown, message);
        }

        /// <summary>Emits a warning.</summary>
        protected void EmitWarning(GrammarSourceRange sourceInfo, string message)
        {
            if (message == null) throw new ArgumentNullException("message");
            if (Message != null) Message(this, false, sourceInfo, message);
        }

        /// <summary>Emits a warning.</summary>
        protected void EmitWarning(string messageFormat, params object[] args)
        {
            if (messageFormat == null) throw new ArgumentNullException("messageFormat");
            if (Message != null) Message(this, false, GrammarSourceRange.Unknown, String.Format(messageFormat, args));
        }

        /// <summary>Emits a warning.</summary>
        protected void EmitWarning(GrammarSourceRange sourceInfo, string messageFormat, params object[] args)
        {
            if (messageFormat == null) throw new ArgumentNullException("messageFormat");
            if (Message != null) Message(this, false, sourceInfo, String.Format(messageFormat, args));
        }
    }
}
